#include <driver/uart.h>
#include <esp_log.h>
#include <esp_check.h>

#include "utilities.h"
#include "uart.h"

#ifndef BUF_SIZE
#define BUF_SIZE (1024)
#endif // !BUF_SIZE

void uart_initialize(int uart_num, int baudrate, int tx_io_num, int rx_io_num) {
    const uart_config_t config = {
        .baud_rate = baudrate,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .source_clk = UART_SCLK_APB,
    };
    ESP_ERROR_CHECK(uart_driver_install(uart_num, BUF_SIZE * 2, BUF_SIZE * 2, 20, NULL, 0));
    ESP_ERROR_CHECK(uart_param_config(uart_num, &config));
    ESP_ERROR_CHECK(uart_set_pin(uart_num, tx_io_num, rx_io_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
}
